<?php
class auth
{
	function auth($app_key,$app_scrept,$back_url)
	{
		$this->app_key=$app_key;
		$this->app_scrept=$app_scrept;
		$this->back_url=$back_url;
		$this->session=new session_operator_native();
		$this->session->session_start();

	}
	function login($location=true)
	{
		$state=md5(uniqid(rand(), TRUE));
		$scope="get_user_info,add_share,list_album,add_album,upload_pic,add_topic,add_one_blog,add_weibo";
		$this->session->set('state',$state);
		$this->session->set('scope',$scope);
		$login_url = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=" 
        . $this->app_key . "&redirect_uri=" . urlencode($this->back_url)
        . "&state=" . $state
        . "&scope=".$scope;
		if($location)
		{
			header("Location:$login_url");
		}
		else
		{
			return $login_url;
		}
	}
	function getOpenId($code)
	{
		$this->getAccessToken($code);
		$access_token=$this->session->get('access_token');
		$graph_url = "https://graph.qq.com/oauth2.0/me?access_token=".$access_token;
		$response  = file_get_contents($graph_url);
		if (strpos($response, "callback") !== false)
		{
			$lpos = strpos($response, "(");
			$rpos = strrpos($response, ")");
			$response  = substr($response, $lpos + 1, $rpos - $lpos -1);
		}
		$user = json_decode($response);
		if (isset($user->error))
		{
			$this->halt['error']=$user->error;
			$this->halt['error_desc']=$user->error_description;
		}
		$this->session->set('openid',$user->openid);
		return $response;
		
	}
	function getAccessToken($code)
	{
		$token_url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&"
            . "client_id=" . $this->app_key. "&redirect_uri=" . urlencode($this->back_url)
            . "&client_secret=" . $this->app_scrept. "&code=" .$code;
		$response = file_get_contents($token_url);
        if (strpos($response, "callback") !== false)
        {
            $lpos = strpos($response, "(");
            $rpos = strrpos($response, ")");
            $response  = substr($response, $lpos + 1, $rpos - $lpos -1);
        }
		
        $msg = json_decode($response);
        if (isset($msg->error))
		{
			$this->halt['error']=$msg->error;
			$this->halt['error_desc']=$msg->error_description;
		}
		$params = array();
        parse_str($response, $params);
		$this->session->set('access_token',$params["access_token"]);
		return $response;
	}
	function getUserInfo($code)
	{
		$this->getOpenId($code);
		$access_token=$this->session->get('access_token');
		$openid=$this->session->get('openid');
		$user_url = "https://graph.qq.com/user/get_user_info?"
        . "access_token=" .$access_token
        . "&oauth_consumer_key=" .$this->app_key
        . "&openid=" . $openid
        . "&format=json";

		$user_message = file_get_contents($user_url);
		if(function_exists('json_decode'))
		{
			$result=(array)json_decode($user_message);
			$result['access_token']=$access_token;
			return $result;
		}
		else
		{
			include "inc/json.class.php";
			$json = new Services_JSON();
			$result=(array)$json->decode($user_message);
			$result['access_token']=$access_token;
			return $result;
		}
		
	}
}